﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using QD.Tools;
using QD.LopPlat.UI.Code.DbContent;
using QD.LopPlat.UI.Model.Sys;
using System.Linq.Expressions;

namespace QD.LopPlat.UI.Code
{
    public class ValidLicense : LicenseBase
    {
        public static ValidLicense Instance
        {
            get
            {
                return new ValidLicense();
            }
        }

        public override string PrivateKey
        {
            get
            {
                return @"<RSAKeyValue><Modulus>4ncc3V909ZKAeAYXSKxtNrOGZxkkrW4R8kvpOVpaIs9+Q0nTTgR7Wn2Fy/nslc3e/5a0VY85816Tcx1TPCMFOGQS7bJ0vlfSOeE2WULnE/CN6MIb7dC6gGx5kvYEE3+HaTNUO8M9SwfBft0YZqd4vvy4QdFlQURGrWSfkb6nW3U=</Modulus><Exponent>AQAB</Exponent><P>+dv1ZO1MvjRYHpsdwbXUAAh+30jQzRfZlrUr5MG9ovw6TTbOGeL9aQma2ipFFyF3Pwq7cq3YMCGeT4oZY425lw==</P><Q>6Af3ZWeUzFR3tWzsMVKv8aAO+8Vf5SMJVaEYrxfnovNNeqDXD0iwBJuB57cPEVvqvU4BJYE9vWf3drxfDGg80w==</Q><DP>ksT7XCLsvpn+3VOe4igN9UsIQ98BEEifPyEDPKf13mStVqgH3zrwQqAqv1avKQw2gOir/YxS/srwtKZZjZo11w==</DP><DQ>3LHPFy2JczwYTIEYuuU6ImmqNsEKMAi+NSswLkJfeTdQB4Z6Z7gsa4V57urIMyupP3u1TXX7Qm9kChSrl0KCLw==</DQ><InverseQ>X8yepYZmfP0ST6QO2hX3VG+GEg/coRRgFCkuK6oOlxwBPodV0drpaiF2GMfuHqI2pRB0mi4i6yObwq0wDyJqGA==</InverseQ><D>Vp1a91oeiBv3sbAWsK9t9+lywrTNn91E1Ohus4ZYig7VClROknzJVwNCLJESiPsbyqJvp3enoOqvt2vkXVBBmqbw5kZhhVgot3yCFUiFZYqGWx2a1hW3llwfZMfL1auGaOts08bqbN4389fX2OWjKt9NKU2H58x9du6TwkekNnM=</D></RSAKeyValue>";
            }
            set
            {

            }
        }

        /// <summary>
        /// 根据数据库注册信息获取软件序列号
        /// </summary>
        /// <returns></returns>
        public override string GetSoftVersionByDb(string pName)
        {
            DbQfDevPlat db = new DbQfDevPlat();

            License mod = db.License.FirstOrDefault(p => p.PName == pName);

            if (mod == null || string.IsNullOrEmpty(mod.Register))
                return string.Empty;
            else
            {
                string softId = DecryptionRSA(mod.Register);
                return softId;
            }
        }

        /// <summary>
        /// 根据数据库注册信息获取软件截止日期
        /// </summary>
        /// <returns></returns>
        public override DateTime? GetEndDateByDb(string pName)
        {
            DbQfDevPlat db = new DbQfDevPlat();

            License mod = db.License.FirstOrDefault(p => p.PName == pName);

            if (mod == null || string.IsNullOrEmpty(mod.EndDate))
                return null;
            else
            {
                string endDateStr = DecryptionRSA(mod.EndDate);
                string[] licese = endDateStr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (licese.Length != 2)
                {
                    throw new Exception("平台有效期验证错误，请不要修改平台注册信息！");
                }

                string s_softId = GetSoftVersionByServcie();
                if (licese[0] != s_softId)
                {
                    throw new Exception("平台有效期验证错误，请不要修改平台注册信息！");
                }

                return Convert.ToDateTime(licese[1]);
            }
        }

        /// <summary>
        /// 注册软件
        /// </summary>
        /// <param name="databaseCode">组织名</param>
        /// <param name="pName">主机名</param>
        /// <param name="licenseValue"></param>
        /// <returns></returns>
        public override void SetSoftVersion(string pName, string licenseValue)
        {
            DbQfDevPlat db = new DbQfDevPlat();
            License mod = db.License.FirstOrDefault(p => p.PName == pName);

            if (mod == null)
            {
                mod = new License();
                mod.PName = pName;
                mod.Register = licenseValue;
                db.License.Add(mod);
            }
            else
            {
                mod.Register = licenseValue;
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 注册有效期
        /// </summary>
        /// <param name="databaseCode">组织名</param>
        /// <param name="pName">主机名</param>
        /// <param name="licenseValue"></param>
        /// <returns></returns>
        public override void SetSoftEndDate(string pName, string licenseValue)
        {
            DbQfDevPlat db = new DbQfDevPlat();
            License mod = db.License.FirstOrDefault(p => p.PName == pName);

            if (mod == null)
            {
                mod = new License();
                mod.PName = pName;
                mod.EndDate = licenseValue;
                db.License.Add(mod);
            }
            else
            {
                mod.EndDate = licenseValue;
            }
            db.SaveChanges();
        }
    }
}
